package com.example.medicalequipment.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.medicalequipment.R;
import com.example.medicalequipment.controller.UserController;
import com.example.medicalequipment.entity.User;

public class RegisterActivity extends AppCompatActivity {
    private EditText etUsername, etPassword, etConfirmPassword, etContact;
    private RadioGroup rgUserType;
    private RadioButton rbNormal, rbRepair;
    private Button btnRegister;
    private UserController userController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        etUsername = findViewById(R.id.etUsername);
        etPassword = findViewById(R.id.etPassword);
        etConfirmPassword = findViewById(R.id.etConfirmPassword);
        etContact = findViewById(R.id.etContact);
        rgUserType = findViewById(R.id.rgUserType);
        rbNormal = findViewById(R.id.rbNormal);
        rbRepair = findViewById(R.id.rbRepair);
        btnRegister = findViewById(R.id.btnRegister);

        userController = new UserController(this);

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = etUsername.getText().toString();
                String password = etPassword.getText().toString();
                String confirmPassword = etConfirmPassword.getText().toString();
                String contact = etContact.getText().toString();
                int userType = rbNormal.isChecked() ? User.TYPE_NORMAL : User.TYPE_REPAIR;

                if (username.isEmpty() || password.isEmpty() || confirmPassword.isEmpty() || contact.isEmpty()) {
                    Toast.makeText(RegisterActivity.this, "请填写所有字段", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!password.equals(confirmPassword)) {
                    Toast.makeText(RegisterActivity.this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }

                User user = new User(username, password, contact, userType);
                userController.register(user, new UserController.RegisterCallback() {
                    @Override
                    public void onRegisterResult(long result) {
                        if (result != -1) {
                            Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(RegisterActivity.this, "注册失败，用户名可能已存在", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }
}